﻿using KoalaStudio.BookshopManager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace ColorPicker
{
    public partial class CPForm : Form
    {
        bool isRGB565 = false;
        public CPForm()
        {
            InitializeComponent();
        }

        /// <summary>
        ///  16进制颜色值
        /// </summary>
        /// <returns></returns>
        public string GetColorHexHtml()
        {
            return CPTool.GetColorHexHtml(Control.MousePosition.X, Control.MousePosition.Y);
        }

        public Color GetColor() {

            return CPTool.GetArgb(Control.MousePosition.X, Control.MousePosition.Y);
        }

        /// <summary>
        ///  获取鼠标的位置
        /// </summary>
        /// <returns></returns>
        public Point getPoint()
        {
            return new Point()
            {
                X = Control.MousePosition.X,
                Y = Control.MousePosition.Y
            };
        }

        private void CPForm_Load(object sender, EventArgs e)
        {
            var screen = Screen.FromPoint(new Point(Cursor.Position.X, Cursor.Position.Y));
            var x = screen.WorkingArea.X + screen.WorkingArea.Width - this.Width;
            var y = screen.WorkingArea.Y + screen.WorkingArea.Height - this.Height;
            this.Location = new Point(x, y);

            //注册热键Ctrl+B，Id号为101。HotKey.KeyModifiers.Ctrl也可以直接使用数字2来表示。
            HotKey.RegisterHotKey(Handle, 101, HotKey.KeyModifiers.Ctrl, Keys.X);
            this.TopMost = true;
        }

        ///// <summary>
        /////  监听快捷键
        ///// </summary>
        ///// <param name="sender"></param>
        ///// <param name="e"></param>
        //private void CPForm_KeyDown(object sender, KeyEventArgs e)
        //{
        //    if ((e.KeyCode == Keys.X) && e.Control)
        //    {
        //        //隐藏程序窗口
        //        Graphics g = this.CreateGraphics();
        //        g.FillRectangle(new SolidBrush(this.GetColor()), 60, 30, 100, 100);
        //        string color = this.GetColorHexHtml();
        //        //this.colorVal.Text = string.Format("颜色值：{0}", color);
        //        //Clipboard.SetDataObject(color);//保存到剪贴板
        //    }

        //}

        private void CPForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            //注销Id号为100的热键设定
            HotKey.UnregisterHotKey(Handle, 101);
        }

        /// 重载FromA中的WndProc函数
        /// 监视Windows消息
        /// 重载WndProc方法，用于实现热键响应
        /// 
        /// 
        protected override void WndProc(ref Message m)
        {
            const int WM_HOTKEY = 0x0312;
            //按快捷键 
            switch (m.Msg)
            {
                case WM_HOTKEY:
                    switch (m.WParam.ToInt32())
                    {
                        case 100:    //按下的是Shift+S       
                            break;
                        case 101:    //按下的是Ctrl+B
                                     //隐藏程序窗口
                            Color myColor = GetColor();
                            colorValue.Text = ColorTranslator.ToHtml(myColor);//真实的颜色
                            string color = ColorTranslator.ToHtml(Rgb2Rgb565(myColor));
                            this.currentColorTb.Text = string.Format("{0}", color);        //此处填写快捷键响应代码  
                            break;
                        case 102:    //按下的是Alt+D
                                     //此处填写快捷键响应代码
                            break;
                    }
                    break;
            }
            base.WndProc(ref m);
        }

        private Color Rgb2Rgb565(Color myColor)
        {
            Color newColor = myColor;
            if (isRGB565)
            {
                int colorR = (newColor.R & 0xF8) | ((newColor.R & 0x38) >> 3);// 高5位有效,补偿5位中的低三位
                int colorG = (newColor.G & 0xFC) | ((newColor.G & 0x0C) >> 2);//高6位有效,补偿6位中的低两位
                int colorB = (newColor.B & 0xF8) | ((newColor.B & 0x38) >> 3);//高5位有效,补偿5位中的低三位
                newColor = Color.FromArgb(colorR, colorG, colorB);
            }
            return newColor;
        }

        private void colorValue_TextChanged(object sender, EventArgs e)
        {
            if (!Regex.IsMatch(colorValue.Text, "#[0-9a-fA-F]{6}")) return;
            try
            {
                Color cColor = ColorTranslator.FromHtml(colorValue.Text);
                int colorR = cColor.R & 0xF8;// 高5位有效
                int colorG = cColor.G & 0xFC;//高6位有效
                int colorB = cColor.B & 0xF8;//高5位有效
               
                int RGB565Value = (colorR << 8) | (colorG << 3) | (colorB >> 3);

                RGB565tb.Text = "#" + RGB565Value.ToString("X4");
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                //MessageBox.Show(ex.Message);
            }
        }

        private void RGB565_CheckedChanged(object sender, EventArgs e)
        {
            //RadioButton tRb = sender as RadioButton;
            if(RGB565Rb.Checked == false)
            {
                isRGB565 = false;//不做任何处理
                currentColorTb.Text = colorValue.Text;
            }
            else
            {
                isRGB565 = true;
                if (string.IsNullOrEmpty(colorValue.Text)) return;
                if (!Regex.IsMatch(colorValue.Text, "#[0-9a-fA-F]{6}")) return;
                Color cColor = ColorTranslator.FromHtml(colorValue.Text);
               
                Color newColor = Rgb2Rgb565(cColor);
                currentColorTb.Text = ColorTranslator.ToHtml(newColor);
            }
        }

        private void currentColorTb_TextChanged(object sender, EventArgs e)
        {
            if (!Regex.IsMatch(currentColorTb.Text, "#[0-9a-fA-F]{6}")) return;
            try
            {
                Color cColor = ColorTranslator.FromHtml(currentColorTb.Text);
                Graphics g = this.CreateGraphics();
                g.FillRectangle(new SolidBrush(cColor), 60, 30, 100, 100);
                lable1.BackColor = cColor;
                lable2.BackColor = cColor;
            }
            catch(Exception ex)
            {
                System.Diagnostics.Debug.WriteLine(ex.Message);
                //MessageBox.Show(ex.Message);
            }
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            linkLabel1.LinkVisited = true;
            Process.Start("https://gitee.com/822360096/color-picker/tree/master/ColorPicker/bin/Release/");
        }
    }
}
